"use strict";

var _assign = require("babel-runtime/core-js/object/assign");

var _assign2 = _interopRequireDefault(_assign);

var _promise = require("babel-runtime/core-js/promise");

var _promise2 = _interopRequireDefault(_promise);

var _stringify = require("babel-runtime/core-js/json/stringify");

var _stringify2 = _interopRequireDefault(_stringify);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var read = require("./read.js");
const shequ_index = (req, res, next) => {
    read.shequ_index(function (err, list) {

        if (err) console.log(err);
        let json = {
            code: 0,
            error: "",
            data: {
                shequ_list: list,
                follow_shequ: list,
                hot_notice: []
            }
            // console.log(list);
        };res.send(json);
    });
};
const doc_doc_list = (req, res, next) => {
    //req.body.shequ_id,req.body.page
    read.doc_doc_list(req, function (err, list) {
        if (err) console.log(err);
        // console.log(list)
        list.map((value, index) => {
            value.img_list = value.img_list.split(",");
        });
        // console.log(list)
        let json = {
            code: 0,
            error: "",
            data: {
                list
            }
        };
        res.send(json);
    });
};
const doc_doc_detail = (req, res, next) => {
    //req.body.shequ_id,req.body.page
    read.doc_doc_detail(req.doc_id, function (err, list) {
        if (err) return console.log(err);
        let json = {
            code: 0,
            error: "",
            data: {
                doc: JSON.parse((0, _stringify2.default)(list))[0],
                user: {
                    headimgurl: "http://wx.qlogo.cn/mmopen/aoHF5icic2Az9rUKgDwRPTKvoJau6l83LgcJ4fK1EOoQyCB0TzuJTURa9zyeiciaeriaXEiam1NRe1kEOWfP80A0JsO9OTUJ5TzoibM/0",
                    id: "1",
                    nickname: "小贱~"
                },
                hot: [{
                    hd_id: "2",
                    hd_type: "2",
                    id: "29",
                    is_hd_label: "1",
                    title: "#来评论#来评论吧"
                }, {
                    hd_id: "1",
                    hd_type: "1",
                    id: "1",
                    is_hd_label: "1",
                    title: "#发帖活动#1这是第一篇帖子！！[哈哈]"
                }],
                shequ: {
                    icon: "http://yxfile.3k.com/2017/03/02/upload_xvhxgc3bbrnj35szu31qda9et5rb658j.gif",
                    id: "1",
                    name: "星辰奇缘"
                }
            }
            // json.data.doc.content = "<View><Text>dfdfdfdfdfdfdfd</Text><Image    style={styles.logo}    source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}/></View>";
        };res.send(json);
    });
};
const doc_doc_detail2 = (doc_id, fn) => {
    return new _promise2.default(function (resolver, reject) {
        read.doc_doc_detail(doc_id, function (err, list) {
            if (err) {
                reject(err);
            }
            let json = {
                code: 0,
                error: "",
                data: {
                    doc: JSON.parse((0, _stringify2.default)(list))[0],
                    user: {
                        headimgurl: "http://wx.qlogo.cn/mmopen/aoHF5icic2Az9rUKgDwRPTKvoJau6l83LgcJ4fK1EOoQyCB0TzuJTURa9zyeiciaeriaXEiam1NRe1kEOWfP80A0JsO9OTUJ5TzoibM/0",
                        id: "1",
                        nickname: "小贱~"
                    },
                    hot: [{
                        hd_id: "2",
                        hd_type: "2",
                        id: "29",
                        is_hd_label: "1",
                        title: "#来评论#来评论吧"
                    }, {
                        hd_id: "1",
                        hd_type: "1",
                        id: "1",
                        is_hd_label: "1",
                        title: "#发帖活动#1这是第一篇帖子！！[哈哈]"
                    }],
                    shequ: {
                        icon: "http://yxfile.3k.com/2017/03/02/upload_xvhxgc3bbrnj35szu31qda9et5rb658j.gif",
                        id: "1",
                        name: "星辰奇缘"
                    }
                }
            };
            resolver(json);
        });
    });
};
const comment_comment_list = (req, res, next) => {
    let p = new _promise2.default(function (resolver, reject) {
        // if ({})
        //resolver({ a: "aaa" })
        // else
        //     reject(error)
    });
    console.log(p, "-------------p");
    let userIcon = {}; //缓存回复用户头像信息
    //获取列表的回复
    function getList_list(data) {
        let tmpCount = 0;
        return new _promise2.default(function (resolver, reject) {
            data.map((item, index) => {
                let reqForm = (0, _assign2.default)(req, { parent_id: item.id });
                read.comment_comment_list_list(reqForm).then(list => {
                    //在这里加工
                    let replyList = JSON.parse((0, _stringify2.default)(list));
                    replyList.forEach((replyItem, replyindex) => {
                        userIcon[index + '_' + replyindex] = replyItem.author_id;
                    });
                    item.ext = replyList;
                    tmpCount++;
                    if (tmpCount == data.length) {
                        resolver({ list: data, replyUId: userIcon });
                    }
                });
            });
        });
    }
    //列表头像获取以及处理
    function commentListUserOpt(item) {
        let userIcon = {}; //缓存回复用户头像信息
        let tmpArray = [];
        let tmpVar = 0;
        item.forEach((replyItem, index) => {
            userIcon[index] = replyItem.author_id;
        });

        return new _promise2.default(function (resolver, reject) {
            for (let ux in userIcon) {
                read.user(userIcon[ux], ux).then(userInfo => {

                    item[userInfo['query']].user = {
                        headimgurl: userInfo.data[0].headimgurl,
                        is_office: userInfo.data[0].is_office,
                        nickname: userInfo.data[0].nickname,
                        uid: userInfo.data[0].uid
                    };
                    tmpVar++;
                    if (tmpVar == item.length) {
                        resolver(item);
                    }
                });
            };
        });
    }

    let json = {
        code: 0,
        error: "",
        data: {
            list: []

        }
        //获取评论列表数据
    };let datafromcommentList = read.comment_comment_list(req);
    try {
        datafromcommentList.then(data => {

            // console.log(data)
            //查询评论列表回复
            return getList_list(data);
        }).then(item => {

            //
            commentListUserOpt(item.list).then(UserOptData => {
                json.data.list = UserOptData;
                let tmpArray = [];
                return new _promise2.default(function (resolver, reject) {
                    let tmpVar = 0;
                    for (let u in item.replyUId) {
                        tmpArray.push(item.replyUId[u]);
                    };
                    for (let u in item.replyUId) {
                        read.user(item.replyUId[u], u).then(userInfo => {
                            let index = userInfo.query.split("_");
                            json.data.list[index[0]].ext[index[1]].nickname = userInfo.data[0].nickname;
                            tmpVar++;
                            if (tmpVar == tmpArray.length) {
                                res.send(json);
                            }
                        });
                    };
                });
            });
        });
    } catch (e) {}
    //console.log(e)


    // read.comment_comment_list(req, function(err, list) {

    //     if (err) return console.log(err);
    //     // let data = JSON.parse(JSON.stringify(list));
    //     console.log(list)
    //     let comment_list_list = {};
    //     let tmpCount = 0;
    //     let getCommint = new Promise(function(resolver, reject) {
    //         data.map((item, index) => {
    //             let reqForm = Object.assign(req, { parent_id: item.id })
    //             read.comment_comment_list_list(req, function(err, list) {
    //                 if (err) {
    //                     reject(error);
    //                 }
    //                 // comment_list_list["ext" + item.id] = {};
    //                 // comment_list_list["ext" + item.id].ext = JSON.stringify(list);
    //                 item.ext = JSON.parse(JSON.stringify(list));
    //                 tmpCount++;
    //                 if (tmpCount == data.length) {
    //                     resolver(data)
    //                 }

    //             });
    //         })
    //     })

    //     getCommint.then((item) => {
    //         console.log(item);
    //         let json = {
    //             code: 0,
    //             error: "",
    //             data: {
    //                 list: data,
    //             }
    //         }
    //         res.send(json);
    //     }).catch(() => {

    //     })


    // })
};
module.exports = {
    shequ_index,
    doc_doc_list,
    doc_doc_detail,
    comment_comment_list,
    doc_doc_detail2
};